Quantitative Methodology (UPF)
Plain files:
.csv, .csv2, .tsv, .txt.Program files:
.xlsx, .xls..Rdata..dta..sav, .spss.Comma Separated Values (csv).
,) separate variables..) separate decimals.Comma Separated Values 2 (csv).
;) separate variables.,) separate decimals.Tab-Separated Values 2 (tsv).
.) separate decimals.Other delimiters (txt).
/) separate variables..) separate decimals.= symbol.$ symbol to impeed draging cells.World University Rankings 2023 (Times Higher Education).
Google Spreadsheets:
Microsoft Excel
getwd(), dir()1:10, sample(1:10, 2), d <- 1#, space.objects(), ls(), search()General rules for creating objects:
^, !, $, @, +, -, /, *..).Vector structure
A vector is formed by a chain of values. It is created with the function c().
c(value1, value2, value3, value4...)
Dataframe structure
A dataframe is formed by several vectors. It is created with the function tibble().
tibble(vector1, vector2, vector3, vector4...)
Two ways of creating a dataframe:
country <- c("Colombia", "Japan", "Germany", "Chile", "New Zealand")
year <- as.integer(c(2022, 2021, 2021, 2021, 2020))
date <- as.Date(c("2022/05/29", "2021/10/31", "2021/09/26", "2021/11/21", "2020/10/17"))
turnout <- c(54.98, 55.97, 76.58, 47.33, 82.24)
continent <- factor(c("America", "Asia", "Europe", "America", "Oceania"))
presidential <- c(TRUE, FALSE, FALSE, TRUE, FALSE)
elections <- tibble(country, year, date, turnout, continent, presidential)elections <- tibble(country = c("Colombia", "Japan", "Germany", "Chile", "New Zealand"),
year = as.integer(c(2022, 2021, 2021, 2021, 2020)),
date = as.Date(c("2022/05/29", "2021/10/31", "2021/09/26", "2021/11/21", "2020/10/17")),
turnout = c(54.98, 55.97, 76.58, 47.33, 82.24),
continent = factor(c("Europe", "Asia", "Europe", "America", "Oceania")),
presidential = c(TRUE, FALSE, FALSE, TRUE, FALSE))Vector in a dataframe
Use of brackets
Function structure
A function is formed by one or several arguments. It is created with the function function().
function(argument1, argument2 ...)
We can easily create a function:
And apply it to any object:
readr.Applied normally to a dataframe:
Applied normally to a vector:
Help!
R is impossible to use without help.
? before a function: e.g. ?sample.Some functions with (normally) more than one argument:
sample()
seq()
rep()
Normally, when they are related to the working environment.
ls()
installed.packages()
search()
getwd()
Quantitative Methodology (UPF)